﻿using AjaxControlToolkit;
using ASP;
using IPMS.AccessAuth;
using IPMS.BLL;
using IPMS.Sort;
using IPMS.Common;
using System;
using System.Configuration;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using Resources;
using System.Web;
using SysOperationLayer.DBHandle;
using System.Data;
using SysComm;

public partial class SysUpdatesHistory : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        string strCulture = ConfigurationManager.AppSettings["DefaultCulture"].ToString();

        if (Session["Culture"] != null)
        {
            strCulture = Session["Culture"].ToString();
        }

        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(strCulture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(strCulture);
        base.InitializeCulture();
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!SysUtil.IsAdministratorOrOperatorUserType())
        {
            Response.Redirect("ErrorPage/NotAllowed.aspx");
            return;
        }
        else
        {
            if (!IsPostBack)
            {
                ((Label)Master.FindControl("lblMstTitle")).Text = "| " + ISitemap.SysMaintenance.ToString() + "| " + ISitemap.UpdateHistory.ToString();
                ddlTableName.Items.Add(new ListItem(""));

                for (int i = 0; i < SysComm.SysUtil.m_sys_tables.Length; i++)
                {
                    ddlTableName.Items.Add(new ListItem(SysComm.SysUtil.m_sys_tables[i]));
                }

                txtDateFrom.Text = DateTime.Now.Date.ToString("yyyy-MM-dd");
                txtDateTo.Text = txtDateFrom.Text;
                GetOperateList("", txtDateFrom.Text, txtDateTo.Text);
            }
        }
    }
    
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        GetOperateList(ddlTableName.SelectedItem.Text.Trim(), txtDateFrom.Text.Trim(), txtDateTo.Text.Trim());
    }
    
    protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        UpdatesDB objDB = new UpdatesDB();

        if (sender == gvInsertResults)
        {
            gvInsertResults.PageIndex = e.NewPageIndex;
            GetInsertList(objDB, ddlTableName.SelectedItem.Text.Trim(), txtDateFrom.Text.Trim(), txtDateTo.Text.Trim());
        }
        else if (sender == gvDeleteResults)
        {
            gvDeleteResults.PageIndex = e.NewPageIndex;
            GetDeleteList(objDB, ddlTableName.SelectedItem.Text.Trim(), txtDateFrom.Text.Trim(), txtDateTo.Text.Trim());
        }
        else if (sender == gvUpdateResults)
        {
            gvUpdateResults.PageIndex = e.NewPageIndex;
            GetUpdateList(objDB, ddlTableName.SelectedItem.Text.Trim(), txtDateFrom.Text.Trim(), txtDateTo.Text.Trim());
        }

        objDB.CloseDB();
    }

    private void GetOperateList(string strTablename, string strDateFrom, string strDateTo)
    {
        UpdatesDB objDB = new UpdatesDB();
        GetInsertList(objDB, strTablename, strDateFrom, strDateTo);
        GetDeleteList(objDB, strTablename, strDateFrom, strDateTo);
        GetUpdateList(objDB, strTablename, strDateFrom, strDateTo);
        objDB.CloseDB();
    }
    
    private void GetInsertList(UpdatesDB objDB, string strTablename, string strDateFrom, string strDateTo)
    {
        
        if (!IsEmpty(strDateFrom))
        {
            strDateFrom += " 00:00:00";
        }

        if (!IsEmpty(strDateTo))
        {
            strDateTo += " 23:59:59";
        }

        DataTable dt = objDB.getInsertsList(strTablename, strDateFrom, strDateTo);
        
        if (dt.Rows.Count > 0)
        {
            gvInsertResults.DataSource = dt;
            gvInsertResults.DataBind();
            lblInsertError.Visible = false;
            gvInsertResults.Visible = true;
        }
        else
        {
            lblInsertError.Text = "No Insert History Record!";
            lblInsertError.Visible = true;
            gvInsertResults.Visible = false;
        }
    }
    
    private void GetDeleteList(UpdatesDB objDB, string strTablename, string strDateFrom, string strDateTo)
    {
        if (!IsEmpty(strDateFrom))
        {
            strDateFrom += " 00:00:00";
        }

        if (!IsEmpty(strDateTo))
        {
            strDateTo += " 23:59:59";
        }

        DataTable dt = objDB.getDeleteList(strTablename, strDateFrom, strDateTo);
        
        if (dt.Rows.Count > 0)
        {
            gvDeleteResults.DataSource = dt;
            gvDeleteResults.DataBind();
            lblDeleteError.Visible = false;
            gvDeleteResults.Visible = true;
        }
        else
        {
            lblDeleteError.Text = "No Delete History Record!";
            lblDeleteError.Visible = true;
            gvDeleteResults.Visible = false;
        }
    }
    
    private void GetUpdateList(UpdatesDB objDB, string strTablename, string strDateFrom, string strDateTo)
    {
        if (!IsEmpty(strDateFrom))
        {
            strDateFrom +=" 00:00:00";
        }

        if (!IsEmpty(strDateTo))
        {
            strDateTo += " 23:59:59";
        }

        DataTable dt = objDB.getUpdatesList(strTablename, strDateFrom, strDateTo);
        
        if (dt.Rows.Count > 0)
        {
            gvUpdateResults.DataSource = dt;
            gvUpdateResults.DataBind();
            lblUpdateError.Visible = false;
            gvUpdateResults.Visible = true;
        }
        else
        {
            lblUpdateError.Text = "No Update History Record!";
            lblUpdateError.Visible = true;
            gvUpdateResults.Visible = false;
        }
    }

    public bool IsEmpty(string strValue)
    {
        return strValue == null || strValue == "";
    }
}
